package org.personal.util;

import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.personal.Constants;
import org.personal.entity.Group;
import org.personal.entity.Page;
import org.personal.entity.Portlet;
import org.personal.entity.User;

public class UserUtil 
{
	public boolean hasSystemAdmin(HttpServletRequest request)
	{
		User user=(User) request.getSession().getAttribute(Constants.USER);
		if ((user==null))
		{
			return false;
		}
		List<String> groups=copyList(user.getGroups());
		groups.add(Constants.USER_ALLUSER);
		groups.add(user.getUid());
		Group admins=Group.getAdmins();
		if (hasCommon(admins.getChildren(), groups)) 
		{
			return true;
		}
		return false;
	}
	public boolean hasPageAdmin(HttpServletRequest request)
	{
		User user=(User) request.getSession().getAttribute(Constants.USER);
		String pageId=request.getParameter("pageId");
		Page page=(Page) request.getAttribute(Constants.PAGE);
		if (page==null)
		{
			page=(Page) request.getSession().getAttribute(Constants.PAGE+"_"+pageId);
		}
		if ((user==null)||(page==null))
		{
			return false;
		}
		List<String> groups=copyList(user.getGroups());
		groups.add(Constants.USER_ALLUSER);
		groups.add(user.getUid());
		if (hasCommon(page.getAdminUsers(), groups)) 
		{
			return true;
		}
		return false;
	}
	public boolean hasPageEdit(HttpServletRequest request)
	{
		User user=(User) request.getSession().getAttribute(Constants.USER);
		String pageId=request.getParameter("pageId");
		Page page=(Page) request.getAttribute(Constants.PAGE);
		if (page==null)
		{
			page=(Page) request.getSession().getAttribute(Constants.PAGE+"_"+pageId);
		}
		if ((user==null)||(page==null))
		{
			return false;
		}
		List<String> groups=copyList(user.getGroups());
		groups.add(Constants.USER_ALLUSER);
		groups.add(user.getUid());
		if (hasCommon(page.getEditUsers(), groups)) 
		{
			return true;
		}
		return false;
	}
	public boolean hasPageRead(HttpServletRequest request)
	{
		User user=(User) request.getSession().getAttribute(Constants.USER);
		String pageId=request.getParameter("pageId");
		Page page=(Page) request.getAttribute(Constants.PAGE);
		if (page==null)
		{
			page=(Page) request.getSession().getAttribute(Constants.PAGE+"_"+pageId);
		}
		if ((user==null)||(page==null))
		{
			return false;
		}
		List<String> groups=copyList(user.getGroups());
		groups.add(Constants.USER_ALLUSER);
		groups.add(Constants.USER_ANONYMOUS);
		groups.add(user.getUid());
		if (hasCommon(page.getUsers(), groups)) 
		{
			return true;
		}
		return false;
	}
	public boolean hasPortletAdmin(HttpServletRequest request,Portlet portlet)
	{
		User user=(User) request.getSession().getAttribute(Constants.USER);
		if ((user==null)||(portlet==null))
		{
			return false;
		}
		List<String> groups=copyList(user.getGroups());
		groups.add(Constants.USER_ALLUSER);
		groups.add(user.getUid());
		if (hasCommon(portlet.getAdminUsers(), groups)) 
		{
			return true;
		}
		return false;
	}
	public boolean hasPortletEdit(HttpServletRequest request,Portlet portlet)
	{
		User user=(User) request.getSession().getAttribute(Constants.USER);
		if ((user==null)||(portlet==null))
		{
			return false;
		}
		List<String> groups=copyList(user.getGroups());
		groups.add(Constants.USER_ALLUSER);
		groups.add(user.getUid());
		if (hasCommon(portlet.getEditUsers(), groups)) 
		{
			return true;
		}
		return false;
	}
	public boolean hasPortletRead(HttpServletRequest request,Portlet portlet)
	{
		User user=(User) request.getSession().getAttribute(Constants.USER);
		if ((user==null)||(portlet==null))
		{
			return false;
		}
		List<String> groups=copyList(user.getGroups());
		groups.add(Constants.USER_ALLUSER);
		groups.add(Constants.USER_ANONYMOUS);
		groups.add(user.getUid());
		if (hasCommon(portlet.getUsers(), groups)) 
		{
			return true;
		}
		return false;
	}
	
	public boolean hasCommon(List<String> list1,List<String> list2)
	{
		if ((list1==null)||(list2==null)||(list1.size()==0)||(list2.size()==0))
		{
			return false;
		}
		for (int i = 0; i <list1.size(); i++) 
		{
			if (list2.contains(list1.get(i)))
			{
				return true;
			}
		}
		return false;
	}
	public List copyList(List list)
	{
		List tempList=new LinkedList();
		for (int i = 0; i < list.size(); i++) 
		{
			tempList.add(list.get(i));
		}
		return tempList;
	}
}
